package com.example.win.android_banduyujia.ui.spinner;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Spinner;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.peopleactivity.adapter.ListviewAdapter;

import java.util.ArrayList;

/**
 * Created by Win on 2015/9/2.
 */
public class CustomerSpinner extends Spinner implements AdapterView.OnItemClickListener {

    private static SelectDialog  dialog = null;
    private ArrayList<String> lists;
    private static String  text;

    public CustomerSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean performClick() {
        Context  context = getContext();
        final LayoutInflater  inflater = LayoutInflater.from(getContext());
        final View view = inflater.inflate(R.layout.title_formcustom_spinner, null);
        final ListView listview= (ListView) view.findViewById(R.id.title_formcustomspinner_list_spinner);
        ListviewAdapter adapter = new ListviewAdapter(context, getLists());
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(this);
        dialog = new SelectDialog(context, R.style.dialog);
        LayoutParams  params = new LayoutParams(500, LayoutParams.MATCH_PARENT);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        dialog.addContentView(view, params);
        return true;
    }

    public ArrayList<String> getLists() {
        return lists;
    }

    public void setList(ArrayList<String> list) {

        this.lists = list;
    }

    public static String getText() {
        return text;
    }

    public static void setText(String text) {
        CustomerSpinner.text = text;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        setSelection(position);
        setText(lists.get(position));
        if(dialog !=null){
            dialog.dismiss();
            dialog = null;
        }
    }
}
